﻿using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web.Configuration;
using System.Configuration;

namespace HK2009.Common
{
    /// <summary>
    /// Mail Clase
    /// Add by Liyx
    /// 2009-01-16
    /// </summary>
    public class SendMail
    {

        #region Class attribute
        private string mailFrom = "server@special-line.com";//Config.GetAppSetting("Mail_Address");
        private string mailTo;
        private string mailSubject;
        private string mailBody;

        private string userName = "server@special-line.com";//Config.GetAppSetting("Mail_Address");
        private string password = DesEncryption.Decrypt("C39AA9DB3E7C29D9192D81955540563E");//(Config.GetAppSetting("Mail_Password").Trim().Length > 0) ? DesEncryption.Decrypt(Config.GetAppSetting("Mail_Password")) : string.Empty;
        private string smtpServer = "smtp.bbmail.com.hk";//Config.GetAppSetting("Mail_Server");

        private bool isSSL = false;//bool.Parse(Config.GetAppSetting("IsSSL"));
        private static int ports = 25;
       // bool istrue = 25;//int.TryParse(Config.GetAppSetting("Port"),out ports);
        

        ///// <summary>
        ///// Mail From
        ///// </summary>
        //public string MailFrom
        //{
        //    get { return mailFrom; }
        //    set
        //    {
        //        mailFrom = value;
        //    }
        //}
        /// <summary>
        /// Mail To
        /// </summary>
        public string MailTo
        {
            get { return mailTo; }
            set
            {
                mailTo = value;
            }
        }
        /// <summary>
        /// Subject
        /// </summary>
        public string MailSubject
        {
            get { return mailSubject; }
            set
            {
                mailSubject = value;
            }
        }
        /// <summary>
        /// Body
        /// </summary>
        public string MailBody
        {
            get { return mailBody; }
            set
            {
                mailBody = value;
            }
        }
        #endregion

        #region Send
        /// <summary>
        /// Send Mail
        /// </summary>
        public void Send()
        {
            string isPwd = "true";//Config.GetAppSetting("IsPassword");

            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            SmtpClient mySend = new SmtpClient(smtpServer);
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;

            if (isPwd.ToLower().Equals("true"))
            {
                mySend.Credentials = new NetworkCredential(userName, password);
            }

            try
            {
                mySend.Send(myMail);
            }
            catch (System.Net.Mail.SmtpException ex)
            {

            }
        }

        /// <summary>
        /// Send e-mail with a attachment
        /// </summary>
        /// <param name="filePath">the attachment file path</param>
        public void Send(string filePath)
        {
            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            Attachment myData = new Attachment(filePath, MediaTypeNames.Application.Octet);
            ContentDisposition myDisposition = myData.ContentDisposition;
            myDisposition.CreationDate = System.IO.File.GetCreationTime(filePath);
            myDisposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
            myDisposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
            myDisposition.FileName = filePath.Substring(filePath.LastIndexOf('/')).Replace('/', ' ');
            myMail.Attachments.Add(myData);
            SmtpClient mySend = new SmtpClient(smtpServer);
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;
            mySend.Credentials = new NetworkCredential(userName, password);
            try
            {
                mySend.Send(myMail);
            }
            catch (System.Net.Mail.SmtpException ex)
            {

            }
            finally
            {
                myData.Dispose();
            }
        }

        /// <summary>
        /// Send e-mail with multiple attachments 
        /// </summary>
        /// <param name="filePathList">the attachments file path</param>
        public void Send(string[] filePathList)
        {
            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            for (int i = 0; i < filePathList.Length; i++)
            {
                Attachment myData = new Attachment(filePathList[i], MediaTypeNames.Application.Octet);
                ContentDisposition myDisposition = myData.ContentDisposition;
                myDisposition.CreationDate = System.IO.File.GetCreationTime(filePathList[0]);
                myDisposition.ModificationDate = System.IO.File.GetLastWriteTime(filePathList[0]);
                myDisposition.ReadDate = System.IO.File.GetLastAccessTime(filePathList[0]);
                myMail.Attachments.Add(myData);
            }
            SmtpClient mySend = new SmtpClient(smtpServer);
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;
            mySend.Credentials = new NetworkCredential(userName, password);
            try
            {
                mySend.Send(myMail);
            }
            catch { }
            finally
            {
                myMail.Attachments.Dispose();
            }
        }
        #endregion

        #region Mass-mailing
        /// <summary>
        /// Mass-mailing
        /// </summary>
        /// <param name="mailToList">Email Group</param>
        public void SendAll(string[] mailToList)
        {
            for (int i = 0; i < mailToList.Length; i++)
            {
                MailTo = mailToList[i];
                Send();
            }
        }

        /// <summary>
        /// Mass-mailing with multiple attachments 
        /// </summary>
        /// <param name="addressList">Email Group</param>
        /// <param name="filePath">the attachments file path</param>
        public void SendAll(string[] mailToList, string filePath)
        {
            for (int i = 0; i < mailToList.Length; i++)
            {
                MailTo = mailToList[i];
                Send(filePath);
            }
        }
        #endregion

    }
}
